package com.example.online_order.conrtoller;

import com.example.online_order.mapper.EvalateMapper;
import com.example.online_order.pojo.Evalate;
import com.example.online_order.pojo.Result;
import com.example.online_order.service.EvalateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/eva")
@CrossOrigin
@Slf4j
public class EvalateController {

//    查询（全表查询  按用户名查询 按菜品名称查询 模糊查询） 删除，新增 修改（追评不修改原来的只追加）
    @Autowired
    EvalateService  evalateService;

    @Autowired
    EvalateMapper evalateMapper;

    @PostMapping("/addEva")
    public Result addeva(@RequestBody Evalate evalate){
        Integer res = evalateService.addEva(evalate);
        if (res != null ){
            return new Result(evalate);
        }else{
            return  Result.fail("evalation already",500);
        }
    }

    @DeleteMapping("/delEva/{id}")
    public Result delAdmin(@PathVariable("id") Integer id){
        Integer res = evalateService.deleteEva(id);
        if (res != null ){
            return new Result(id);
        }else{
            return  Result.fail("delete failed",444);
        }
    }

    @PutMapping("/updateEva")
    public Result updateEva(@RequestBody Evalate evalate){
        Integer res = evalateService.updateEva(evalate);
        if (res != null ){
            return new Result("update success");
        } else{
            return  Result.fail("更新失败",444);
        }
    }

    @GetMapping("/getAllEva")
    public Result getAllEva(){
        return new Result(evalateService.selectAllEva());
    }

    @GetMapping("/like/userName/{userName}/mealName/{mealName}")
    public Result selectEva(@PathVariable("userName") String userName, @PathVariable("mealName") String mealName){
        List<Evalate> eva = evalateService.selectevaLikeUserMeal(userName, mealName);
        if (eva.isEmpty()){
            return Result.fail("该查询结果为空",404);
        } else {
            return Result.success(eva);
        }
    }
}
